package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneOfferInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferObjInstRelDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotNull;
import com.iwhalecloud.bss.kite.dataservice.mapper.inst.OfferInstHisMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.KiteInstRelDTO;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.scene.AbstractInstService;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * <Description> <br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @date 2019-08-02 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.service.scene.query <br>
 */
@Service("querySceneOfferInstService")
@Primary
public class QuerySceneOfferInstService extends AbstractInstService implements IQuerySceneOfferInstService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    OfferInstHisMapper offerInstHisMapper;

    /**
     * 获取接入品实例所属的销售品实例(成员明细关系)
     *
     * @param custId 客户id
     * @param prodInstId 接入品实例id
     * @return 套餐&可选包&促销包销售品实例列表
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> listOfferInstsByProdInstId(@KiteNotBlank String custId,
        @KiteNotBlank String prodInstId) {
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listOfferInstsByProdInstId(custId, prodInstId);
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList));
    }

    /**
     * 根据销售品实例id获取销售品实例对象
     * @param custId
     * @param offerInstId
     * @return
     */
    @Override
    @Validated
    public KiteOfferInst getOfferInst(@KiteNotBlank String custId,
        @KiteNotBlank String offerInstId) {
        return querySceneInstService.getOfferInst(custId, offerInstId);
    }

    /***
     * 查询礼包下所有产品的明细；
     * @param sceneInstId
     * @param offerInstId
     * @param custId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferObjInstRelDTO>> listOfferObjInstRels(String sceneInstId, String offerInstId, String custId) {

        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInstsFromOfferInst(custId, offerInstId);
        List<OfferObjInstRelDTO> returnList = new ArrayList();

        for (KiteProdInst prodInst : kiteProdInstList) {
            List<KiteInstRelDTO> instRels = querySceneInstService.listInstRelsByZInst(custId, InstType.PROD_INST, prodInst.getProdInstId(), Arrays.asList(InstType.MAIN_OFFER_INST, InstType.PROMOTION_OFFER_INST));
            KiteInstRelDTO instRel = instRels.stream().filter(rel -> Objects.equals(querySceneInstService.getOfferInst(custId, rel.getaInstId()).getInstType(), InstType.PROMOTION_OFFER_INST)).findFirst().orElse(null);
            if (Objects.isNull(instRel)) {
                instRel = instRels.stream().filter(rel -> Objects.equals(querySceneInstService.getOfferInst(custId, rel.getaInstId()).getInstType(), InstType.MAIN_OFFER_INST)).findFirst().orElse(null);
            }
            if (!Objects.isNull(instRel)) {
                returnList.add(CopyToDtoUtil.copyToOfferObjInstRelDTO(instRel, custId));
            }
        }
        return ErrorConsts.SUCCESS.getResult(returnList);

    }

    /**
     * 统计产品实例下失效的可选包实例数量
     * @param offerInstId
     * @param offerIds
     * @param custId
     * @param onProduct
     * @return
     */
    @Override
    public Long countUnsubscribedOfferInst(@KiteNotBlank String offerInstId, @KiteNotNull Set<String> offerIds, @KiteNotBlank String custId, boolean onProduct) {
        return offerInstHisMapper.countUnsubscribedOfferInst(offerInstId, offerIds, custId, onProduct);
    }

    /**
     * 统计当前套餐实例/促销包实例/基础销售品实例下失效的可选包实例数量
     * @param offerInstId
     * @param offerIds
     * @param custId
     * @return
     */
    @Override
    public Long countUnsubcribedOfferInstByCompInstId(@KiteNotBlank String offerInstId, @KiteNotNull Set<String> offerIds, @KiteNotBlank String custId) {
        return offerInstHisMapper.countUnsubcribedOfferInstByCompInstId(offerInstId, offerIds, custId);
    }
}
